<?php

namespace Index\Controller;

use Think\Controller;
use Index\ViewModel;
use Index\Service;

class LaborController extends CommonController {

    public function laborlist() {
        $appname_arr = $this->getapplist();
        $appname_enum = $this->getenumbyarr($appname_arr);
        $app_code = $this->app_code;
        $begintime = trim(I('txtbegintime'));
        $endtime = trim(I('txtendtime'));
        $keyword = trim(I('txtkeyword'));
        $appcode = trim(I('ddlapp', '0'));
        $gradeid = trim(I('gradeid'));

        if (!empty($app_code)) {
            $map['app_flag'] = $app_code;
        } else {
            if (!empty($appcode)) {
                $map['app_flag'] = $appcode;
            } else if ($appcode == '') {
                $map['app_flag'] = array(array('exp', 'is null'), array('eq', ''), 'or');
            }
        }
        $map['status'] = '1';
        if (!empty($gradeid)) {
            $map['grade_id'] = $gradeid;
        }
        if (!empty($begintime) || !empty($endtime)) {
            if (!empty($begintime) && !empty($endtime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
            } else if (!empty($begintime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
            } else if (!empty($endtime)) {
                $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
            }
        }
        if (!empty($keyword)) {
            $where['labor.name'] = array('like', '%' . $keyword . '%');
            $where['labor.address'] = array('like', '%' . $keyword . '%');
            $where['labor.linkman'] = array('like', '%' . $keyword . '%');
            $where['labor.tel'] = array('like', '%' . $keyword . '%');
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        $viewModel = new ViewModel\LaborViewModel();
        $total = $viewModel->where($map)->count();
        $row = '10';
        $page = new Service\PageService($total, $row);
        $fpage = $page->fpage();
        $data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->order('ctime desc')->select();
        foreach ($data as $key => $value) {
            $code_text = '<img alt="未找到图片" src="' . $data[$key]['code'] . '" class="code" />';
            $user_count = M('chat_labor_user')->where(array('labor_id' => $value['id']))->count();
//            $user_count_text = empty($user_count) ? '0' : '<a href="javascript:void(0);">' . $user_count . '</a>';
            $user_count_text = empty($user_count) ? '0' : '<a href="javascript:void(0);" onclick="seemember(' . $data[$key]['id'] . ')" title="查看成员">' . $user_count . '</a>';
            $data[$key]['totalmoney'] = (int) $value['totalmoney'];
            $data[$key]['w_money'] = (int) $value['w_money'];
            $data[$key]['user_count'] = $user_count;
            $data[$key]['user_count_text'] = $user_count_text;
            $data[$key]['code_text'] = $code_text;
            $data[$key]['appname'] = $appname_enum[$data[$key]['app_flag']];
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
        }

        $search = array('appsel' => $appcode, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
        $model = M('chat_labor_grade');
        $where['status'] = '1';
        $where['app_flag'] = $map['app_flag'];
        $data_labor_grade = $model->where($where)->field('id,grade')->order('ctime desc')->select();
        $grade_id = $gradeid;
        foreach ($data_labor_grade as $key => $value) {
            $data_labor_grade[$key]['value'] = $value['id'];
            $data_labor_grade[$key]['text'] = $value['grade'];
            if ($grade_id == $value['id']) {
                $data_labor_grade[$key]['selected'] = "selected='selected'";
            }
        }
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'search' => $search,
            'app_code' => $app_code,
            'data_labor_grade' => $data_labor_grade,
            'apparr' => $this->getapplist()
        ));
        $this->display();
    }

    public function laboradd() {
        $app_code = $this->app_code;
        if (empty($app_code)) {
            $app_code = I('app_code');
        }
        if (IS_POST) {
            $id = I('id');
            $name = trim(I('name'));
            $gradeid = trim(I('gradeid'));
            $adress = trim(I('address'));
            $linkman = trim(I('linkman'));
            $tel = trim(I('tel'));
            $pwd = trim(I('pwd'));
            if (empty($name)) {
                $response = array('ret' => '1002', 'code' => '工会名称不能为空！');
                return $this->ajaxReturn($response, 'json');
            }
            if (empty($gradeid)) {
                $response = array('ret' => '1002', 'code' => '工会等级不能为空！');
                return $this->ajaxReturn($response, 'json');
            }
            if (empty($adress)) {
                $response = array('ret' => '1002', 'code' => '所在地区不能为空！');
                return $this->ajaxReturn($response, 'json');
            }
            if (empty($linkman)) {
                $response = array('ret' => '1002', 'code' => '联系人不能为空！');
                return $this->ajaxReturn($response, 'json');
            }
            if (!empty($tel) && !preg_match("/1[34578]{1}\d{9}$/", $tel)) {
                $response = array('ret' => '1002', 'code' => '手机号码格式不正确！');
                return $this->ajaxReturn($response, 'json');
            }
            if (!empty($app_code)) {
                $map['app_flag'] = $app_code;
            } else {
                $map['app_flag'] = array(array('exp', 'is null'), array('eq', ''), 'or');
            }
            $map['status'] = array('neq', '2');
            $map['name'] = $name;
            if (!empty($id)) {
                $map['id'] = array('neq', $id);
            }
            $model = M('chat_labor');
            if ($model->where($map)->count()) {
                $response = array('ret' => '1002', 'code' => '对不起，该工会已存在！');
                return $this->ajaxReturn($response, 'json');
            }
            $data['name'] = $name;
            $data['grade_id'] = $gradeid;
            $data['address'] = $adress;
            $data['linkman'] = $linkman;
            $data['tel'] = $tel;
            $data['pwd'] = md5(empty($pwd) ? '123456' : $pwd);
            if (empty($id)) {
                $data['status'] = '1';
                $data['ctime'] = time();
                if (!empty($app_code)) {
                    $data['app_flag'] = $app_code;
                }
            }
            if (!empty($id)) {
                if ($model->where(array('id' => $id))->save($data)) {
                    $response = array('ret' => '1001', 'code' => '修改成功！');
                } else {
                    $response = array('ret' => '1001', 'code' => '修改成功！');
                }
            } else {
                $id = $model->add($data);
                if ($id) {
                    //生成二维码
                    $success = new \Index\Service\QRcode();
//                    $val = $success::png('http://wkmkd.imkedou.com/Index/kedou?id=' . $id, './Public/img/code' . $id . '.png');
//                    $code = 'http://kedouadm.86tudi.com/Public/img/code' . $id . '.png';
//                    $code = 'http://pan.baidu.com/share/qrcode?w=150&h=150&url=http://wkmkd.imkedou.com/Index/kedou?id=' . $id;
//                    $model->where(array('id' => $id))->setField('code', $code);
                    $val = $success::png('http://localhost:8080/mitanapi/html/down', './Public/img/code' . $id . '.png');

                    $picRemoteName='code' . $id . '.png';
                    $filePath='./Public/img/'.$picRemoteName;
//                    $code = 'http://testvideoadm.86tudi.com/Public/img/code' . $id . '.png';
                    $content=$this->imgupload($filePath,$picRemoteName);
                    
                    if($content['ret']='1001'){
                        $model->where(array('id' => $id))->setField('code', $content['data']);
                        $response = array('ret' => '1001', 'code' => '添加成功！');
                    }else{
                        $response = array('ret' => '1002', 'code' => '添加失败！');
                    }
                } else {
                    $response = array('ret' => '1002', 'code' => '添加失败！');
                }
            }
            return $this->ajaxReturn($response, 'json');
            die;
        }
        $status_enum = getenumbykey('state');
        if (!empty($app_code)) {
            $map['app_flag'] = $app_code;
        } else {
            $map['app_flag'] = array(array('exp', 'is null'), array('eq', ''), 'or');
        }
        $model = M('chat_labor_grade');
        $map['status'] = '1';
        $data_labor_grade = $model->where($map)->field('id,grade')->order('ctime desc')->select();
        $id = I('id');
        $columnname = '添加工会';
        $btnname = '添加';
        if (!empty($id)) {
            $columnname = '修改工会';
            $btnname = '修改';
            $model = M('chat_labor');
            $data = $model->where(array('id' => $id))->find();
            $grade_id = $data['grade_id'];
            $data['status_text'] = $status_enum[$data['status']];
            $data['ctime'] = date('Y-m-d H:i', $data['ctime']);
        }
        foreach ($data_labor_grade as $key => $value) {
            $data_labor_grade[$key]['value'] = $value['id'];
            $data_labor_grade[$key]['text'] = $value['grade'];
            if ($grade_id == $value['id']) {
                $data_labor_grade[$key]['selected'] = "selected='selected'";
            }
        }
        $this->assign('app_code',$app_code);
        $this->assign('columnname', $columnname);
        $this->assign('btnname', $btnname);
        $this->assign('id', $id);
        $this->assign('data', $data);
        $this->assign('data_labor_grade', $data_labor_grade);
        $this->display();
    }

    public function labordelete() {
        $id = I('id');
        if (!empty($id)) {
            $model = M('chat_labor_user');
            $labor_user_count = $model->where(array('labor_id' => $id))->count();
            if (!empty($labor_user_count)) {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，请先删除该工会下的成员！'));
            }
            $model = M('chat_labor');
            $data = $model->where(array('id' => $id))->find();
            if ($data) {
                $data['status'] = '2';
                $result = $model->save($data);
                if ($result) {
                    return $this->ajaxReturn(array('ret' => '1001', 'code' => '删除成功！'));
                } else {
                    return $this->ajaxReturn(array('ret' => '1002', 'code' => '删除失败！'));
                }
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该工会可能已被删除！'));
            }
        }
    }

    public function laborgradelist() {
        $appname_arr = $this->getapplist();
        $appname_enum = $this->getenumbyarr($appname_arr);
        $app_code = $this->app_code;
        $begintime = trim(I('txtbegintime'));
        $endtime = trim(I('txtendtime'));
        $keyword = trim(I('txtkeyword'));
        $appcode = trim(I('ddlapp', '0'));
        $map['status']=array('neq', '2');
        if (!empty($app_code)) {
            $map['app_flag'] = $app_code;
        } else {
            if (!empty($appcode)) {
                $map['app_flag'] = $appcode;
            } else if ($appcode == '') {
                $map['app_flag'] = array(array('exp', 'is null'), array('eq', ''), 'or');
            }
        }
        if (!empty($begintime) || !empty($endtime)) {
            if (!empty($begintime) && !empty($endtime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
            } else if (!empty($begintime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
            } else if (!empty($endtime)) {
                $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
            }
        }
        if (!empty($keyword)) {
            $where['grade'] = array('like', '%' . $keyword . '%');
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        $model = M('chat_labor_grade');
        $total = $model->where($map)->count();
        $row = '10';
        $page = new Service\PageService($total, $row);
        $fpage = $page->fpage();
        $data = $model->where($map)->limit($page->listfirst, $page->listRows)->order('ctime desc')->select();
        foreach ($data as $key => $value) {
            $data[$key]['appname'] = $appname_enum[$data[$key]['app_flag']];
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
        }
        $search = array('appsel' => $appcode, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'search' => $search,
            'app_code' => $app_code,
            'apparr' => $this->getapplist()
        ));
        $this->display();
    }

    public function laborgradeadd() {
        $app_code = $this->app_code;
        if (empty($app_code)) {
            $app_code = I('app_code');
        }
        if (IS_POST) {
            if (empty($app_code)) {
                $app_code = I('app_code');
            }
            $id = I('id');
            $grade = trim(I('grade'));
            $money = trim(I('money'));
            $ratio = trim(I('ratio'));
            $num = trim(I('num'));
            $number = trim(I('number'));
            if (empty($grade)) {
                $response = array('ret' => '1002', 'code' => '等级名称不能为空！');
                return $this->ajaxReturn($response, 'json');
            }
            if (!($money >= 0)) {
                $response = array('ret' => '1002', 'code' => '管理费输入内容不合法！');
                return $this->ajaxReturn($response, 'json');
            }
            if (!($ratio >= 0)) {
                $response = array('ret' => '1002', 'code' => '分红比例输入内容不合法！');
                return $this->ajaxReturn($response, 'json');
            }
            if (!($num >= 0)) {
                $response = array('ret' => '1002', 'code' => '直播号数量输入内容不合法！');
                return $this->ajaxReturn($response, 'json');
            }
            if (!($number >= 0)) {
                $response = array('ret' => '1002', 'code' => '限定数量输入内容不合法！');
                return $this->ajaxReturn($response, 'json');
            }
            if (!empty($app_code)) {
                $map['app_flag'] = $app_code;
            } else {
                $map['app_flag'] = array(array('exp', 'is null'), array('eq', ''), 'or');
            }
            $map['status'] = array('neq', '2');
            $map['grade'] = $grade;
            if (!empty($id)) {
                $map['id'] = array('neq', $id);
            }
            $model = M('chat_labor_grade');
            if ($model->where($map)->count()) {
                $response = array('ret' => '1002', 'code' => '对不起，该工会已存在！');
                return $this->ajaxReturn($response, 'json');
            }
            $data['grade'] = $grade;
            $data['money'] = $money;
            $data['ratio'] = $ratio;
            $data['num'] = $num;
            $data['number'] = $number;
            if (empty($id)) {
                $data['status'] = '1';
                $data['ctime'] = time();
                if (!empty($app_code)) {
                    $data['app_flag'] = $app_code;
                }
            }
            if (!empty($id)) {
                if ($model->where(array('id' => $id))->save($data)) {
                    $response = array('ret' => '1001', 'code' => '修改成功！');
                } else {
                    $response = array('ret' => '1001', 'code' => '修改成功！');
                }
            } else {
                if ($model->add($data)) {
                    $response = array('ret' => '1001', 'code' => '添加成功！');
                } else {
                    $response = array('ret' => '1002', 'code' => '添加失败！');
                }
            }
            return $this->ajaxReturn($response, 'json');
            die;
        }
        $status_enum = getenumbykey('state');
        if (!empty($app_code)) {
            $map['app_flag'] = $app_code;
        } else {
            $map['app_flag'] = array(array('exp', 'is null'), array('eq', ''), 'or');
        }
        $id = I('id');
        $columnname = '添加工会等级';
        $btnname = '添加';
//        $data = array('money' => '0', 'ratio' => '0', 'num' => '0', 'number' => '0');
        if (!empty($id)) {
            $columnname = '修改工会等级';
            $btnname = '修改';
            $model = M('chat_labor_grade');
            $data = $model->where(array('id' => $id))->find();
            $data['status_text'] = $status_enum[$data['status']];
            $data['ctime'] = date('Y-m-d H:i', $data['ctime']);
        }
        $this->assign('columnname', $columnname);
        $this->assign('app_code',$app_code);
        $this->assign('btnname', $btnname);
        $this->assign('id', $id);
        $this->assign('data', $data);
        $this->display();
    }

    public function laborgradedelete() {
        $id = I('id');
        if (!empty($id)) {
            $model = M('chat_labor');
            $labor_count = $model->where(array('grade_id' => $id))->count();
            if (!empty($labor_count)) {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，请先删除该工会等级下的工会！'));
            }
            $model = M('chat_labor_grade');
            $data = $model->where(array('id' => $id))->find();
            if ($data) {
                $data['status'] = '2';
                $result = $model->save($data);
                if ($result) {
                    return $this->ajaxReturn(array('ret' => '1001', 'code' => '删除成功！'));
                } else {
                    return $this->ajaxReturn(array('ret' => '1002', 'code' => '删除失败！'));
                }
            } else {
                return $this->ajaxReturn(array('ret' => '1002', 'code' => '对不起，该工会等级可能已被删除！'));
            }
        }
    }

    public function laboruserlist() {
        $appname_arr = $this->getapplist();
        $appname_enum = $this->getenumbyarr($appname_arr);
        $sex_enum = getenumbykey('sex');
        $sex_arr = getarrbyenum($sex_enum);
        $state_enum = getenumbykey('userlogin_state');
        $state_arr = getarrbyenum($state_enum);
        $id_state_enum = getenumbykey('user_auth');
        $id_state_arr = getarrbyenum($id_state_enum);
        $flag_enum = getenumbykey('flag');
        $flag_arr = getarrbyenum($flag_enum);
        $app_code = $this->app_code;
        $begintime = trim(I('txtbegintime'));
        $endtime = trim(I('txtendtime'));
        $keyword = trim(I('txtkeyword'));
        $appcode = trim(I('ddlapp', '0'));
        if (!empty($app_code)) {
            $map['app_flag'] = $app_code;
        } else {
            if (!empty($appcode)) {
                $map['app_flag'] = $appcode;
            } else if ($appcode == '') {
                $map['app_flag'] = array(array('exp', 'is null'), array('eq', ''), 'or');
            }
        }
        if (!empty($begintime) || !empty($endtime)) {
            if (!empty($begintime) && !empty($endtime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
            } else if (!empty($begintime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
            } else if (!empty($endtime)) {
                $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
            }
        }
        if (!empty($keyword)) {
            $where['grade'] = array('like', '%' . $keyword . '%');
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        $map = '';
        $model = M('chat_labor_user');
        $sql = "select count(*) as count from labor_user a left join chat_userinfo b on a.usre_uid = b.user_uid left join labor c on a.labor_id = c.id where 1=1 " . $map;
        $total = $model->query($sql);
        $row = '10';
        $page = new Service\PageService($total['0']['count'], $row);
        $fpage = $page->fpage();
        $sql = "select a.ctime as labor_user_ctime, a.money as labor_user_money, b.*, c.name as labor_name from labor_user a left join chat_userinfo b on a.usre_uid = b.user_uid left join labor c on a.labor_id = c.id where 1=1 " . $map . " limit " . $page->listfirst . "," . $page->listRows;
        $data = $model->query($sql);
        foreach ($data as $key => $value) {
            $user_logo_text = '<img class="img" alt="未找到图片" src="' . $data[$key]['user_logo'] . '"/>';
            $data[$key]['user_logo_text'] = $user_logo_text;
            $data[$key]['user_sex_text'] = empty($data[$key]['user_sex']) ? $sex_enum['3'] : $sex_enum[$data[$key]['user_sex']];
            $data[$key]['user_gold_text'] = (int) $data[$key]['user_gold'];
            $data[$key]['user_flag_text'] = $data[$key]['state'] == '2' ? ($data[$key]['user_flag_switch'] == '1' ? $flag_enum[$data[$key]['user_flag']] : '禁止上架') : '';
            $data[$key]['user_state_text'] = $state_enum[$data[$key]['is_lock']];
            $data[$key]['user_idstate_text'] = empty($data[$key]['state']) ? $id_state_enum['1'] : $id_state_enum[$data[$key]['state']];
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
        }
        $search = array('appsel' => $appcode, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'search' => $search,
            'app_code' => $app_code,
            'apparr' => $this->getapplist()
        ));
        $this->display();
    }

    public function laborearninglist() {
        $appname_arr = $this->getapplist();
        $appname_enum = $this->getenumbyarr($appname_arr);
        $app_code = $this->app_code;
        $begintime = trim(I('txtbegintime'));
        $endtime = trim(I('txtendtime'));
        $keyword = trim(I('txtkeyword'));
        $appcode = trim(I('ddlapp', '0'));
        if (!empty($app_code)) {
            $map['app_flag'] = $app_code;
        } else {
            if (!empty($appcode)) {
                $map['app_flag'] = $appcode;
            } else if ($appcode == '') {
                $map['app_flag'] = array(array('exp', 'is null'), array('eq', ''), 'or');
            }
        }
        if (!empty($begintime) || !empty($endtime)) {
            if (!empty($begintime) && !empty($endtime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
            } else if (!empty($begintime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
            } else if (!empty($endtime)) {
                $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
            }
        }
        if (!empty($keyword)) {
            $where['labor_earning.orderno'] = array('eq', $keyword);
            $where['labor.name'] = array('eq', $keyword);
            $where['chat_userinfo.user_id'] = array('eq', $keyword);
            $where['chat_userinfo.user_phone'] = array('like', '%' . $keyword . '%');
            $where['chat_userinfo.user_name'] = array('like', '%' . $keyword . '%');
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        $viewModel = new ViewModel\LaborearningViewModel();
        $total = $viewModel->where($map)->count();
        $row = '10';
        $page = new Service\PageService($total, $row);
        $fpage = $page->fpage();
        $data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->order('ctime desc')->select();
        foreach ($data as $key => $value) {
            $data[$key]['summoney'] = (int) $value['summoney'];
            $data[$key]['money'] = (int) $value['money'];
            $labor_grade_text = M('chat_labor_grade')->where(array('id' => $value['labor_grade']))->getField('grade');
            $data[$key]['labor_grade_text'] = $labor_grade_text;
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
        }
        $search = array('appsel' => $appcode, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'search' => $search,
            'app_code' => $app_code,
            'apparr' => $this->getapplist()
        ));
        $this->display();
    }

    public function laborwithdarwallist() {  //工会提现记录
        $labor_status_enum = getenumbykey('labor_withdrawal_status');
        $labor_status_arr = getarrbyenum($labor_status_enum);
        $labor_returnstatus_enum = getenumbykey('labor_withdrawal_returnstatus');
        $labor_returnstatus_arr = getarrbyenum($labor_returnstatus_enum);
        $labor_bankclass_enum = getenumbykey('labor_withdrawal_bankclass');
        $labor_bankclass_arr = getarrbyenum($labor_bankclass_enum);
        $app_code = $this->app_code;
        $begintime = trim(I('txtbegintime'));
        $endtime = trim(I('txtendtime'));
        $keyword = trim(I('txtkeyword'));
        $appcode = trim(I('ddlapp', '0'));
        $bank = trim(I('addbank'));
        $withdrawal = trim(I('addwithdrawal'));
        $statu = trim(I('addstatus', '1'));

        if (!empty($app_code)) {
            $map['app_flag'] = $app_code;
        } else {
            if (!empty($appcode)) {
                $map['app_flag'] = $appcode;
            } else if ($appcode == '') {
                $map['app_flag'] = array(array('exp', 'is null'), array('eq', ''), 'or');
            }
        }
        if (!empty($begintime) || !empty($endtime)) {
            if (!empty($begintime) && !empty($endtime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
            } else if (!empty($begintime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
            } else if (!empty($endtime)) {
                $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
            }
        }
        if (!empty($bank)) {
            $map['bankclass'] = $bank;
        }
        if (!empty($withdrawal)) {
            $map['returnstatus'] = $withdrawal;
        }
        if (!empty($statu)) {
            $map['status'] = $statu;
        }
        if (!empty($keyword)) {
            $where['labor.name'] = array('like', '%' . $keyword . '%');
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        $viewModel = new ViewModel\LaborwithdrawalViewModel();
        $total = $viewModel->where($map)->count();
        $row = '10';
        $page = new Service\PageService($total, $row);
        $fpage = $page->fpage();
        $data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->order('ctime desc')->select();
        foreach ($data as $key => $value) {
            $data[$key]['status_text'] = $labor_status_enum[$data[$key]['status']];
            $data[$key]['returnstatus_text'] = $labor_returnstatus_enum[$data[$key]['returnstatus']];
            $data[$key]['bankclass_text'] = $labor_bankclass_enum[$data[$key]['bankclass']];
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
            $data[$key]['money'] = (int) $data[$key]['money'];
        }
        foreach ($labor_status_arr as $key => $value) {
            if (in_array($value['value'], array(1, 2, 4))) {
                $status_arr[$key]['value'] = $value['value'];
                $status_arr[$key]['text'] = $value['text'];
            }
        }
        $search = array('appsel' => $appcode, 'keyword' => $keyword, 'begintime' => $begintime, 'banksel' => $bank, 'endtime' => $endtime, 'withdrawalsel' => $withdrawal, 'statusel' => $statu);
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'laborstatusarr' => $labor_status_arr,
            'laborreturnstatusarr' => $labor_returnstatus_arr,
            'laborbankclassarr' => $labor_bankclass_arr,
            'status_arr' => $status_arr,
            'search' => $search,
            'app_code' => $app_code,
            'apparr' => $this->getapplist()
        ));
        $this->display();
    }

    public function finishsubmit() {
        $labor_bankclass_enum = getenumbykey('labor_withdrawal_bankclass');
        $app_code = $this->app_code;
        $id = I('id');
        $content = I('content');
        $type = I('type');
        if (!empty($id)) {
            $map['id'] = $id;
            $model = M('chat_labor_withdrawal');
            $data = $model->where($map)->find();
            if ($data) {
                $labor_id = $data['labor_id']; //工会id
                $money = (int) $data['money'];
                $order_id = $data['order_id'];
                $status = $type == '1' ? '2' : '4';
                $lab = $status == '2' ? '操作' : '驳回';
                $data['bank_name'] = $labor_bankclass_enum[$data['bankclass']]; //银行名
                $data['bank_card_no'] = $data['bankaccount']; //银行卡号
                $result = $model->where($map)->save(array('status' => $status, 'txt' => $content, 'ptime' => time()));
                if ($result) {
                    if ($status == '2') {
                        $id = M('chat_labor')->where(array('id' => $labor_id))->getField('id');
                        $tel = M('chat_labor')->where(array('id' => $labor_id))->getField('tel'); //工会联系人手机
                        $user_id = M('chat_userinfo')->where(array('user_phone' => $tel))->getField('user_id');
                        $data = array(
                            'id' => $id,
                            'user_id' => $user_id,
                            'money' => $money,
                            'tel' => $tel,
                            'bankclass' => $data['bankclass'],
                            'status' => $data['status'],
                            'order_id' => $data['order_id'],
                            'bank_name' => $data['bank_name'],
                            'bank_card_no' => $data['bank_card_no'],
                        );
                        $res = $this->withdrawremind($data);
                    }
                    if ($status == '3') {
//                        $map = array();
//                        $map['user_uid'] = $user_uid;
//                        //退回到账户余额
//                        $model = M('chat_userwallet');
//                        $data = $model->where($map)->find();
//                        //                        $balance =  (int)$data['balance'] +  $money * $ration;
////                        newredisadd(33, "userwallet_" . $user_uid, 'balance', $balance);
////                        $model->where($map)->save(array('balance' => $balance));
//                        $money_earn = (int) $data['money_earn'] + $money * $ration;
////                        newredisadd(33, "userwallet_" . $user_uid, 'money_earn', $money_earn);
////                        $model->where($map)->save(array('money_earn' => $money_earn));
//
//                        $totalBalance = $data['balance'] + $money_earn;
//                        //添加账户余额更新日志
//                        $model = M('chat_balancelog');
//                        $data = array(
//                            'user_uid' => $user_uid,
//                            'order_no' => 'TX' . time() . rand(100000, 999999),
//                            'content' => '提现（订单号：' + $order_no + '）被管理员驳回',
//                            'money' => $money * $ration,
//                            'balance' => $totalBalance,
//                            'ratio' => '',
//                            'state' => '1',
//                            'type' => '4',
//                            'toggle' => '2',
//                            'ctime' => time(),
//                            'app_flag' => $app_code,
//                        );
//                        $model->add($data);
                    }
                    $response = array('ret' => '1001', 'code' => $lab . '成功！', 'data' => $status);
                } else {
                    $response = array('ret' => '1002', 'code' => $lab . '失败！', 'data' => $status);
                }
            } else {
                $response = array('ret' => '1002', 'code' => '对不起，该提现记录可能已被删除！');
            }
        } else {
            $response = array('ret' => '1002', 'code' => '无效的操作！');
        }
        $this->ajaxReturn($response, 'json');
    }

    public function withdrawremind($data) {
        $app_code = $this->app_code;
        $id = $data['id'];
        $user_id = $data['user_id'];
        $content = '';
        $type = $data['type'];
        $status = $data['status'];
        $order_id = $data['order_id'];
        $money = $data['money'];
        $account = $data['account'];
        $bank_name = $data['bank_name'];
        $bank_card_no = $data['bank_card_no'];
        $tel = $data['tel'];
        $type = 3;
        switch ($type) {
            case '1':
                $content = $money . '金额已打款至您的支付宝账户【账号：' . $account . '】【订单号：' . $order_id . '】，请您查收！';
                break;
            case '2':
                $content = $money . '金额已打款至您的微信账户【账号：' . $account . '】【订单号：' . $order_id . '】，请您查收！';
                break;
            case '3':
                $content = $money . '金额已打款至您的' . $bank_name . '银行账户【银行卡号：' . $bank_card_no . '】【订单号：' . $order_id . '】，请您查收！';
                break;
            default:
                break;
        }
        if (empty($user_id) || empty($content)) {
            return array('ret' => '1002', 'code' => '操作失败，用户ID或者发送内容为空！');
            exit();
        }
        $appKey = '0vnjpoad0vimz';
        $appSecret = 'yMGaWW6VqHUSb';
        $RongCloud = new \Index\Service\RongCloud($appKey, $appSecret);
        //$arr['operation'] =  'op1';
        //$arr['sourceUserId'] =  '1';
        //$arr['targetUserId'] =  I('uid2');
        $arr['content'] = $content;
        $arr['extra'] = 'helloExtra';
        $string = json_encode($arr);
        $result = $RongCloud->Message()->publishPrivate('54', $user_id, 'RC:TxtMsg', $string, '', '', '', '0', '1', '1');
        $result = json_decode($result, true);
        if ($result['code'] == '200') {
            return array('code' => '1001', 'result' => '操作成功！');
            exit();
        } else {
            return array('code' => '1002', 'result' => '操作失败，请重试！');
            exit();
        }
    }

    public function laborsharelist() {
        $appname_arr = $this->getapplist();
        $appname_enum = $this->getenumbyarr($appname_arr);
        $app_code = $this->app_code;
        $begintime = trim(I('txtbegintime'));
        $endtime = trim(I('txtendtime'));
        $keyword = trim(I('txtkeyword'));
        $appcode = trim(I('ddlapp', '0'));
        if (!empty($app_code)) {
            $map['app_flag'] = $app_code;
        } else {
            if (!empty($appcode)) {
                $map['app_flag'] = $appcode;
            } else if ($appcode == '') {
                $map['app_flag'] = array(array('exp', 'is null'), array('eq', ''), 'or');
            }
        }
        if (!empty($begintime) || !empty($endtime)) {
            if (!empty($begintime) && !empty($endtime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
            } else if (!empty($begintime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
            } else if (!empty($endtime)) {
                $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
            }
        }
        if (!empty($keyword)) {
            $where['labor_sharelog.content'] = array('eq', $keyword);
            $where['labor.name'] = array('eq', $keyword);
            $where['chat_userinfo.user_id'] = array('eq', $keyword);
            $where['chat_userinfo.user_phone'] = array('like', '%' . $keyword . '%');
            $where['chat_userinfo.user_name'] = array('like', '%' . $keyword . '%');
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        $viewModel = new ViewModel\LaborshareViewModel();
        $total = $viewModel->where($map)->count();
        $row = '10';
        $page = new Service\PageService($total, $row);
        $fpage = $page->fpage();
        $data = $viewModel->where($map)->limit($page->listfirst, $page->listRows)->order('ctime desc')->select();
        foreach ($data as $key => $value) {
            $user_logo_text = '<img alt="未找到图片" src="' . $data[$key]['user_logo'] . '" class="img" />';
            $user_info = $this->app_name2 . '号：' . $data[$key]['user_id'] . '<br />用户昵称：' . $data[$key]['user_name'] . '<br />手机号码：' . $data[$key]['user_phone'];
            $data[$key]['user_logo_text'] = $user_logo_text;
            $data[$key]['user_info'] = $user_info;
            $data[$key]['money'] = (int) $value['money'];
            $data[$key]['balance'] = (int) $value['balance'];
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
        }
        $search = array('appsel' => $appcode, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'search' => $search,
            'app_code' => $app_code,
            'apparr' => $this->getapplist()
        ));
        $this->display();
    }

    public function seememberlist() {
        $appname_arr = $this->getapplist();
        $appname_enum = $this->getenumbyarr($appname_arr);
        $sex_enum = getenumbykey('sex');
        $sex_arr = getarrbyenum($sex_enum);
        $state_enum = getenumbykey('userlogin_state');
        $state_arr = getarrbyenum($state_enum);
        $id_state_enum = getenumbykey('user_auth');
        $id_state_arr = getarrbyenum($id_state_enum);
        $flag_enum = getenumbykey('flag');
        $flag_arr = getarrbyenum($flag_enum);
        $app_code = $this->app_code;
        $begintime = trim(I('txtbegintime'));
        $endtime = trim(I('txtendtime'));
        $keyword = trim(I('txtkeyword'));
        $appcode = trim(I('ddlapp', '0'));
        if (!empty($app_code)) {
            $map['app_flag'] = $app_code;
        } else {
            if (!empty($appcode)) {
                $map['app_flag'] = $appcode;
            } else if ($appcode == '') {
                $map['app_flag'] = array(array('exp', 'is null'), array('eq', ''), 'or');
            }
        }
        if (!empty($begintime) || !empty($endtime)) {
            if (!empty($begintime) && !empty($endtime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
            } else if (!empty($begintime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
            } else if (!empty($endtime)) {
                $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
            }
        }
        if (!empty($keyword)) {
            $where['grade'] = array('like', '%' . $keyword . '%');
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        $map = '';

        $map = ' and a.labor_id like ' . I('labor_id');
        $model = M('chat_labor_user');
        $sql = "select count(*) as count from labor_user a left join chat_userinfo b on a.usre_uid = b.user_uid left join labor c on a.labor_id = c.id where 1=1 " . $map;
        $total = $model->query($sql);
        $row = '5';
        $page = new Service\PageService($total['0']['count'], $row);
        $fpage = $page->fpage();
        $sql = "select a.ctime as labor_user_ctime,a.labor_id, a.money as labor_user_money, b.*, c.name as labor_name from labor_user a left join chat_userinfo b on a.usre_uid = b.user_uid left join labor c on a.labor_id = c.id where 1=1 " . $map . " limit " . $page->listfirst . "," . $page->listRows;
        $data = $model->query($sql);
        foreach ($data as $key => $value) {
            $user_logo_text = '<img class="img" alt="未找到图片" src="' . $data[$key]['user_logo'] . '"/>';
            $data[$key]['user_logo_text'] = $user_logo_text;
            $data[$key]['user_sex_text'] = empty($data[$key]['user_sex']) ? $sex_enum['3'] : $sex_enum[$data[$key]['user_sex']];
            $data[$key]['user_gold_text'] = (int) $data[$key]['user_gold'];
            $data[$key]['user_flag_text'] = $data[$key]['state'] == '2' ? ($data[$key]['user_flag_switch'] == '1' ? $flag_enum[$data[$key]['user_flag']] : '禁止上架') : '';
            $data[$key]['user_state_text'] = $state_enum[$data[$key]['is_lock']];
            $data[$key]['user_idstate_text'] = empty($data[$key]['state']) ? $id_state_enum['1'] : $id_state_enum[$data[$key]['state']];
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
        }
        $search = array('appsel' => $appcode, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'search' => $search,
            'app_code' => $app_code,
            'apparr' => $this->getapplist()
        ));
        $this->display();
    }

    public function labortotal(){
        $type = I('type');
        $state_enum = getenumbykey('app_state');
        $state_arr = getarrbyenum($state_enum);
        $state = '1';
        $begintime = trim(I('txtbegintime'));
        $endtime = trim(I('txtendtime'));
        $keyword = trim(I('txtkeyword'));
        switch ($type) {
            case '1':
                $columnname = '添加工会';
                $url='laboradd';
                break;
            case '2':
                $columnname = '添加工会等级';
                $url='laborgradeadd';
                break;
            default:
                break;
        }
        if (!empty($state)) {
            $map['app_state'] = $state;
        }
        if (!empty($begintime) || !empty($endtime)) {
            if (!empty($begintime) && !empty($endtime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), strtotime($endtime) + 60));
            } else if (!empty($begintime)) {
                $map['ctime'] = array('between', array(strtotime($begintime), time() + 60));
            } else if (!empty($endtime)) {
                $map['ctime'] = array('between', array(0, strtotime($endtime) + 60));
            }
        }
        if (!empty($keyword)) {
            $where['id'] = array('eq', $keyword);
            $where['app_flag'] = array('like', '%' . $keyword . '%');
            $where['app_name'] = array('like', '%' . $keyword . '%');
            $where['_logic'] = 'or';
            $map['_complex'] = $where;
        }
        $model = M('chat_systemset');
        $total = $model->where($map)->count();
        $row = '10';
        $page = new Service\PageService($total, $row);
        $fpage = $page->fpage();
        $field = array(
            'id', 'app_flag', 'app_logo', 'app_name', 'app_state', 'ctime'
        );
        $order = 'ctime desc, id desc';
        $data = $model->where($map)->limit($page->listfirst, $page->listRows)->field($field)->order($order)->select();
        foreach ($data as $key => $value) {
            $data[$key]['ctime'] = date('Y-m-d H:i', $data[$key]['ctime']);
        }
        $search = array('statesel' => $state, 'keyword' => $keyword, 'begintime' => $begintime, 'endtime' => $endtime);
        $this->assign(array(
            'type' => $type,
            'data' => $data,
            'fpage' => $fpage,
            'total' => $total,
            'search' => $search,
            'columnname' => $columnname,
            'urllist' => $url,
            'statearr' => $state_arr,
        ));
        $this->display();
    }

    /**
     * 上传图片
     */
    public function imgupload($filePath,$picRemoteName,$type='photos')
    {
        $access_key_id = C('ACCESS_KEY_ID');
        $access_key_secret = C('ACCESS_KEY_SECRET');
        $img_url = C('IMG_URL');
        $img_url2 = C('IMG_URL2');
        $success = new \Vendor\OSS\OssClient($access_key_id, $access_key_secret, $img_url2);
        $bucket = C('BUCKET');
        $object = $type . '/' . $picRemoteName;
        try {
            $success->uploadFile($bucket, $object, $filePath);
            unlink($filePath);
            $img = $img_url . '/' . $object;
            $data['ret'] = '1001';
            $data['code'] = '上传成功！';
            $data['data'] = $img;
            return $data;
        } catch (OssException $e) {
            $data = array('ret' => '1002', 'code' => '远程上传错误！');
            return $data;
        }

    }
}
